import "reflect-metadata";
import { createConnection } from "typeorm";
import { Role } from "../entity/Role";
import { Request, Response } from "express";
import { stages, quests } from "../configs/configs";
import { Stage } from "../entity/Stage";

const moment = require('moment-timezone');
const connR = createConnection("role");

export function roleRoute(app: any) {

    app.get('/api/role/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            let data = await connection.getRepository(Role).findOne(req.params.role_id);
            res.send(data)

        }).catch(error => console.log(error));
    })

    app.post('/api/vip/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            const repository = connection.getRepository(Role);
            let role = await repository.findOne(req.params.role_id);
            role.vip = req.body.value;
            await repository.save(role);

            res.send({ success: true })

        }).catch(error => res.send({ success: false, msg: error }));
    })

    app.post('/api/level/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            let repository = connection.getRepository(Role);
            let role = await repository.findOne(req.params.role_id);
            role.level = req.body.value;
            await repository.save(role);

            res.send({ success: true })

        }).catch(error => res.send({ success: false, msg: error }));
    })

    app.get('/api/stage/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            let data = await connection.getRepository(Stage).findOne(req.params.role_id);
            res.send(data)

        }).catch(error => console.log(error));
    })

    app.post('/api/stage/:role_id', function (req: Request, res: Response) {

        connR.then(async connection => {

            const repository = connection.getRepository(Stage);
            let stage = await repository.findOne(req.params.role_id);
            stage.stage_id = req.body.value;
            await repository.save(stage);

            res.send({ success: true })

        }).catch(error => res.send({ success: false, msg: error }));
    })

    app.get('/api/roles', function (req: Request, res: Response) {

        const pageNum = 20

        let page = req.query.page
        if (!page) { page = 1 }

        connR.then(async connection => {


            let where = {}
            if (req.query.name) {
                where["name"] = req.query.name
            }
            if (req.query.role_id) {
                where["role_id"] = req.query.role_id
            }

            let data = await connection.getRepository(Role).find({ relations: ["stage", "quest", "online"], where: where, order: { role_id: 'DESC' }, skip: (page - 1) * pageNum, take: pageNum });
            data.forEach(element => {
                element.created_at = moment(element.created_at).format();
                element.stage.name = stages[element.stage.stage_id].Name;

                if (quests[element.quest.quest_id]) {
                    element.quest.name = quests[element.quest.quest_id].Name + "(" + String(element.quest.quest_id) + ")";
                }

                if (element.online && element.online.online_time) {
                    element.online.online_time = moment(element.online.online_time).format();
                }
                if (element.online && element.online.offline_time) {
                    element.online.offline_time = moment(element.online.offline_time).format();
                }

            });

            res.send(data)

        }).catch(error => console.log(error));
    })

    app.get('/role', function (req: Request, res: Response) {

        connR.then(async connection => {

            let data = await connection.getRepository(Role).createQueryBuilder().select("COUNT(*) AS c").getRawOne();
            res.render('role/index', { total: data.c })

        }).catch(error => console.log(error));

    })
}
